﻿using d1Project.Modules.Common.do4A.Migrations;
using d1Project.Modules.Common.do4A.Models;
using d1Project.Tools;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace d1Project.Modules.Common.do4A
{
    public class do4ADbContext : DbContext
    {
        static do4ADbContext()
        {
            //Database.SetInitializer<do4ADbContext>(new ApplicationDbInitializer());
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<do4ADbContext, do4ADbConfig>());
        }
        public do4ADbContext()
            : base(DoEnvironment.DefaultDbName)
        {
            this.Configuration.LazyLoadingEnabled = false;
        }

        //-------------------------------------------------------------------------------
        public DbSet<Area> Areas { get; set; }
        public DbSet<AuditLog> AuditLogs { get; set; }
        public DbSet<Dept> Depts { get; set; }
        public DbSet<DeptPwd> DeptPwds { get; set; }
        public DbSet<RolePermission> RolePermissions { get; set; }
        public DbSet<ThirdPartUser> ThirdPartUser2s { get; set; }
        public DbSet<User1> User1s { get; set; }
        public DbSet<User1Pwd> User1Pwds { get; set; }
        public DbSet<User2> User2s { get; set; }
        public DbSet<User2Pwd> User2Pwds { get; set; }
        public DbSet<User3> User3s { get; set; }
        public DbSet<User3Pwd> User3Pwds { get; set; }
        public DbSet<User4> User4s { get; set; }
        public DbSet<User4Pwd> User4Pwds { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<VerificationCode> VerificationCodes { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.HasDefaultSchema("dbo");
        }

        public class ApplicationDbInitializer : CreateDatabaseIfNotExists<do4ADbContext>
        {
            protected override void Seed(do4ADbContext context)
            {
                var user1 = new User1 { Id = "admin", Code = "admin", Name = "管理员", Sex = "0", Birthday = DateTime.Now.ToString("yyyy-MM-dd"), Email = "baiyang@qq.com", MobilePhone = "15604242726", IdCard = "412726199306247155", IsAvailable = "1", DeptId = "0000", CreateTime = DateTime.Now, UpdateTime = DateTime.Now };
                context.User1s.Add(user1);

                var dept = new Dept { Id = "0000", Code = "0000", Name = "总部", Path = "0000", CreateTime = DateTime.Now, UpdateTime = DateTime.Now };
                context.Depts.Add(dept);

                base.Seed(context);
            }
        }

    }
}